 // 计算两点间距离（简化的Haversine公式）
  function calculateDistance(lat1, lon1, lat2, lon2) {
    const R = 6371000; // 地球半径(米)
    const dLat = deg2rad(lat2 - lat1);
    const dLon = deg2rad(lon2 - lon1);
    
    const a = 
      Math.sin(dLat/2) * Math.sin(dLat/2) +
      Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
      Math.sin(dLon/2) * Math.sin(dLon/2);
    
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    const distance = R * c; // 单位：米
    
    return Math.round(distance);
  }
  
  // 角度转弧度
	function deg2rad(deg) {
    return deg * (Math.PI/180)
  }
module.exports = {
  // 获取首页数据
  async getHomeData(latitude, longitude) {
    const db = uniCloud.database()
    const dbCmd = db.command
    
    try {
      // 1. 获取轮播图数据
      const bannerRes = await db.collection('opendb-banner').orderBy('sort', 'asc').get()
      console.log('bannerRes',bannerRes)
      const banners = bannerRes.data.map(item => {
        return {
          id: item._id,
          image: item.bannerfile.url,
          link: item.open_url
        }
      })
      
      // 2. 功能区数据（静态数据）
      const gridItems = [
        {id: 1, icon: 'paperplane', text: '立即寄存', bgColor: '#FF7F00', type: 'deposit'},
        {id: 2, icon: 'calendar', text: '预约寄存', bgColor: '#FF9E40', type: 'reserve'},
        {id: 3, icon: 'shop', text: '寄存点', bgColor: '#FFB870', type: 'depots'},
        {id: 4, icon: 'gift', text: '优惠活动', bgColor: '#FFD2A8', type: 'promo'}
      ]
      
      // 3. 获取附近寄存点
      let nearbyDepots = []
      latitude=latitude || 22.696279
	  longitude=longitude || 114.044205
      if (latitude && longitude) {
        const depotRes = await db.collection('mydb-depot-list')
          .where({
            status: 1, // 启用状态的站点
            location: dbCmd.geoNear({
              geometry: new db.Geo.Point(longitude, latitude),
              maxDistance: 100000, // 10公里范围内
              minDistance: 0
            })
          })
          .orderBy('location', 'asc') // 按距离排序
          .limit(10) // 最多10个
          .get()
        console.log('depotRes',depotRes)
        // 计算距离并格式化数据
        nearbyDepots = depotRes.data.map(depot => {
          // 计算距离（简化计算）
          const depotLng = depot.location.coordinates[0]
          const depotLat = depot.location.coordinates[1]
          const distance = calculateDistance(latitude, longitude, depotLat, depotLng)
          
          // 获取主图
          const mainImage = depot.images && depot.images.length > 0 
            ? depot.images[0]['url']
            : 'https://images.unsplash.com/photo-1544620347-c4fd4a3d5957?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=300'
          
          // 获取营业时间
          let hours = '全天开放'
          if (depot.operating_hours && depot.operating_hours.weekdays) {
            hours = depot.operating_hours.weekdays
          }
          
          return {
            id: depot._id,
            name: depot.name,
            image: mainImage,
            distance: distance < 1000 
              ? `${Math.round(distance)}m` 
              : `${(distance/1000).toFixed(1)}km`,
            address: depot.address || '未知地址',
            price: depot.price || 8,
            size: '中', // 实际应用中应从站点获取箱型信息
            available: depot.available_count || 0,
            hours
          }
        })
      }
      
      return {
        code: 0,
        message: '获取首页数据成功',
        data: {
          banners,
          gridItems,
          nearbyDepots
        }
      }
    } catch (e) {
      console.error('获取首页数据失败:', e)
      return {
        code: 500,
        message: '获取首页数据失败',
        data: null
      }
    }
  },
  
 
}